import { http } from "./request";
export interface GoodsQueryType {
  page?: number;
  limit?: number;
}
export interface UserType {
  id?: string;
  username: string;
  password: string;
  email?: string;
  phone?: string;
}
export interface ResultType {
  id?: number | string;
  uid: number | string;
  gid: number | string;
  time?: string;
  db?: string;
}
export function getGoodsList({
  page = 1,
  limit = 3,
  type,
}: GoodsQueryType & { type: string | number }) {
  return http.get<any>("/goods/getGoodsList", { page, limit, type });
}
export function getSearchGoods({
  page = 1,
  limit = 10,
  name,
}: GoodsQueryType & { name: string }) {
  return http.get<any>("/goods/getSearchGoods", { page, limit, name });
}
export function getCode(name: string, email: string) {
  return http.get<any>("/code/selectCode", { name, email });
}
export function addUser(data: UserType & { code: number | string }) {
  return http.post<any>("/user/addUser", null, {
    data,
    headers: { "Content-Type": "application/json" },
  });
}
export function getUserLogin(data: UserType) {
  return http.get<any>("/user/getUserLogin", {
    username: data.username,
    password: data.password,
  });
}
// id, username, email, phone
export function putUserInfo({ id, username, email, phone }: UserType) {
  return http.put<any>(
    "/user/putUserInfo",
    { id, username, email, phone },
    {
      headers: {
        Authorization: `Bearer ${localStorage.getItem("FRUIT_TOKEN")}`,
      },
    }
  );
}
export function putUserPassword({ oldPwd, newPwd, id }) {
  return http.put<any>("/user/putUserPassword", { oldPwd, newPwd, id });
}
export function addBrowse(data: ResultType) {
  return http.post<any>("/records/addRecords", null, {
    data,
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${localStorage.getItem("FRUIT_TOKEN")}`,
    },
  });
}
export function addLog({ uid, gids, total, time, address }) {
  return http.post<any>("pruchase/addLog", null, {
    data: { uid, gids, total, time, address },
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${localStorage.getItem("FRUIT_TOKEN")}`,
    },
  });
}
export function addRecords({ ids }: { ids: number[] }) {
  return http.post<any>("goods/addRecords", null, {
    data: { ids },
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${localStorage.getItem("FRUIT_TOKEN")}`,
    },
  });
}
export function getUserRecords(id: number, db: string) {
  return http.get<any>(
    "/user/getUserRecords",
    { id, db },
    {
      headers: {
        Authorization: `Bearer ${localStorage.getItem("FRUIT_TOKEN")}`,
      },
    }
  );
}
export function getLogList(uid: number) {
  return http.get<any>(
    "/pruchase/getLogList",
    { uid },
    {
      headers: {
        Authorization: `Bearer ${localStorage.getItem("FRUIT_TOKEN")}`,
      },
    }
  );
}
// /records/getCollectState
export function getCollectState(uid: number, gid: number) {
  return http.get<any>(
    "/records/getCollectState",
    { uid, gid },
    {
      headers: {
        Authorization: `Bearer ${localStorage.getItem("FRUIT_TOKEN")}`,
      },
    }
  );
}
export function getAddr(uid: number) {
  return http.get<any>(
    "/address/getAddress",
    { uid },
    {
      headers: {
        Authorization: `Bearer ${localStorage.getItem("FRUIT_TOKEN")}`,
      },
    }
  );
}
export function setAddr(
  uid: number,
  name: string,
  phone: string,
  address: string
) {
  return http.post<any>("/address/setAddress", null, {
    data: { uid, name, phone, addr: address },
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${localStorage.getItem("FRUIT_TOKEN")}`,
    },
  });
}
export function putAddr(
  id: string,
  name: string,
  phone: string,
  address: string,
  normal: string = "0"
) {
  return http.put<any>(
    "/address/updateAddress",
    { id, name, phone, addr: address, normal },
    {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${localStorage.getItem("FRUIT_TOKEN")}`,
      },
    }
  );
}
export function delAddr(ids: string) {
  return http.delete<any>(
    "/address/delAddress",
    { ids },
    {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${localStorage.getItem("FRUIT_TOKEN")}`,
      },
    }
  );
}
